package com.atguigu.srb.core.controller.api;


import com.atguigu.common.exception.BusinessAssert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.service.BorrowInfoService;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.utils.JwtUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author 龙龙龙
 * @since 2022-01-21
 */
@RestController
@RequestMapping("api/core/borrowInfo")
public class ApiBorrowInfoController {

    @Autowired
    private BorrowInfoService borrowInfoService;

    @ApiOperation("获取借款申请审批状态")
    @GetMapping("/auth/getBorrowInfoStatus")
    public R getBorrowerStatus(HttpServletRequest request){
        String token = request.getHeader("token");
        BusinessAssert.notNull(token, ResponseEnum.LOGIN_AUTH_ERROR);
        Long userId = JwtUtils.getUserId(token);
        BusinessAssert.notNull(userId,ResponseEnum.WEIXIN_FETCH_ACCESSTOKEN_ERROR);
        Integer status = borrowInfoService.getStatusByUserId(userId);
        return R.ok().data("borrowInfoStatus", status);
    }

    @PostMapping("/auth/save")
    public R save(HttpServletRequest request, @RequestBody BorrowInfo borrowInfo){
        String token = request.getHeader("token");
        BusinessAssert.notNull(token, ResponseEnum.LOGIN_AUTH_ERROR);
        Long userId = JwtUtils.getUserId(token);
        BusinessAssert.notNull(userId,ResponseEnum.WEIXIN_FETCH_ACCESSTOKEN_ERROR);
        borrowInfoService.saveBorrowInfo(borrowInfo,userId);
        return R.ok();
    }

    @GetMapping("/auth/getBorrowAmount")
    public R getBorrowAmount(HttpServletRequest request){
        String token = request.getHeader("token");
        BusinessAssert.notNull(token, ResponseEnum.LOGIN_AUTH_ERROR);
        Long userId = JwtUtils.getUserId(token);
        BusinessAssert.notNull(userId,ResponseEnum.WEIXIN_FETCH_ACCESSTOKEN_ERROR);

        BigDecimal borrowAmount =  borrowInfoService.getBorrowAmount(userId);

        return R.ok().data("borrowAmount",borrowAmount);
    }

}

